/*
 * Creative Commons Deed
 * Attribution-NonCommercial-ShareAlike 1.0 Finland
 * 
 * You are free:
 * 
 * * to copy, distribute, display, and perform the work
 * * to make derivative works
 * Under the following conditions:
 * 
 * Attribution. You must attribute the work in the manner specified by the author or licensor.
 * 
 * Noncommercial. You may not use this work for commercial purposes.
 * 
 * Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only 
 * under a license identical to this one.
 * 
 * * For any reuse or distribution, you must make clear to others the license terms of this work.
 * * Any of these conditions can be waived if you get permission from the copyright holder.
 * Your fair use and other rights are in no way affected by the above.
 * This is a human-readable summary of the Legal Code (the full license). 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Net;
using System.Drawing;

namespace PocketLibrary.Storage
{
    [Obsolete]
    public enum FileSystemType
    {
        FileType,
        FolderType,
        UnknownType,
    }

    [Obsolete]
    public class BoxNetFs
    {


        public string UserName = "";
        public string PassWord = "";
        public string Shared = "";
        public string SharedLink = "";
        public string ItemId = "";
        string sid = "";
        public string SessionId
        {
            get { return sid; }
            set { this.sid = value; }
        }
        public string FileId = "";
        public string FileName = "";
        public string KeyWord = "";
        public string Size = "";
        public string Created = "";
        public string UpDated = "";
        public string ThumbNail = "";
        public System.Drawing.Bitmap ThumbNailImage = null;
        public int ThumbSize = 64; // 64x64 thumbnail
        
        public string Name = "";
        public string FolderId = "";
        public string Share = "";

        FileSystemType itemType = FileSystemType.UnknownType;

        public FileSystemType ItemType
        {
            get { return itemType; }
            set { itemType = value; }
        }


        public void DownloadThumbnail()
        {
            try
            {
                if (!ThumbNail.StartsWith("http://"))
                {
                    ThumbNail = "http://www.box.net/" + ThumbNail;
                }

                //http://m.box.net/index.php?rm=box_v2_download_file&file_id=f_8249205&resize=1
                //string mThumb = "http://m.box.net/index.php?rm=box_v2_download_file&file_id=f_";
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ThumbNail);
                req.Method = "GET";
                req.UserAgent = "TaskuBlogi 1.0";

                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

                // Retrieve response stream
                Stream respStream = resp.GetResponseStream();

                // Create local file
                MemoryStream wrtr = new MemoryStream();

                // Allocate byte buffer to hold stream contents
                byte[] inData = new byte[4096];

                // loop through response stream reading each data block
                //  and writing to the local file
                int bytesRead = respStream.Read(inData, 0, inData.Length);
                while (bytesRead > 0)
                {
                    wrtr.Write(inData, 0, bytesRead);
                    bytesRead = respStream.Read(inData, 0, inData.Length);
                }
                this.ThumbNailImage = new Bitmap(wrtr);
                respStream.Close();
                wrtr.Close();
            }
            catch { }
        }

        public void DownloadThumbnailHack()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            sb.Append("<boxnet>");
            sb.Append("<action>bn.getThumbnail</action>");
            sb.Append("<params>");
            sb.Append("<username>" + UserName + "</username>");
            sb.Append("<password>" + PassWord + "</password>");
            sb.Append("<boxthumbnail>");
            sb.Append("<sid>" + SessionId + "</sid>");
            sb.Append("<thumb>" + FileId + "</thumb>");
            if (ThumbSize != 64)
                sb.Append("<size>" + ThumbSize.ToString() + "</size>");
            sb.Append("</boxthumbnail>");
            sb.Append("</params>");
            sb.Append("</boxnet>");
            sendXmlDoc.LoadXml(sb.ToString());
            SendReceiveXml();
            XmlNode base64data = rcvXmlDoc.SelectSingleNode("descendant::data/base64");
            if (base64data != null)
            {
                MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64data.InnerText));
                ThumbNailImage = new Bitmap(ms);
            }
        }

        void SendReceiveXml()
        {
            try
            {
                rcvXmlDoc = new XmlDocument();
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.choomba.org/BNThumbs.aspx");

                req.Method = "POST";
                req.UserAgent = "TaskuBlogi 1.0";
                req.ContentType = "text/xml";

                byte[] data = Encoding.ASCII.GetBytes(sendXmlDoc.InnerXml);
                req.ContentLength = data.Length;
                Stream newstream = req.GetRequestStream();
                newstream.Write(data, 0, data.Length);
                newstream.Close();
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                Stream restream = res.GetResponseStream();

                StringBuilder sbb = new StringBuilder();

                using (StreamReader sr = new StreamReader(restream))
                {
                    while (sr.Peek() != -1)
                    {
                        sbb.Append(sr.ReadToEnd());
                    }
                }
                restream.Close();

                rcvXmlDoc.LoadXml(sbb.ToString());
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (XmlException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


        public override string ToString()
        {
            return FileName;
        }

        protected XmlDocument sendXmlDoc = new XmlDocument();
        protected XmlDocument rcvXmlDoc = new XmlDocument();

    }
}
